<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="utf-8"/>
		<title>生产日报表</title>
		<meta name="viewport" content="width=device-width, initial-scale=1"/>
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta http-equiv="pragma" content="no-cache"/>
		<meta http-equiv="cache-control" content="no-cache"/>
		<meta http-equiv="expires" content="0"/>
		<link rel="stylesheet" href="../4/lumen/bootstrap.css" media="screen"/>
		<link rel="stylesheet" href="../_assets/css/custom.min.css"/>
	</head>
<body>
	
	
	<div class="container">
		<legend>生产日报表</legend>
		<form action="SD2CSV.php" method="post">
			开始时间: <input type="date" name="sjj1"/>
			结束时间: <input type="date" name="sjj2"/>
			<!-- 工單號: <input type="text" name="GongDan"/> -->
			<label><input name="Fruit[]" type="checkbox" value="齐纳" />齐纳 </label> 
			<label><input name="Fruit[]" type="checkbox" value="固晶" />固晶 </label> 
			<label><input name="Fruit[]" type="checkbox" value="焊线" />焊线 </label> 
			<label><input name="Fruit[]" type="checkbox" value="点胶" />点胶 </label> 
			<label><input name="Fruit[]" type="checkbox" value="切割" />切割 </label> 
			<label><input name="Fruit[]" type="checkbox" value="分光" />分光 </label> 
			<label><input name="Fruit[]" type="checkbox" value="编带" />编带 </label> 
			
			<input type="submit" value="导出"/>
			<input type="submit" formaction="WIP2CSV.php" value="WIP表导出"/>
			<br/>(提示:要同時指定開始時間和結束時間才能導出)
		</form>
		
		<br/>
		<br/>
		<div class="form-group row">
			<label class="col-sm-1 col-form-label" for="inputDefault">工序:</label>
			<div class="col-sm-2">
				<select name="WP" class="form-control" id="WP" type="text">
					<option disabled selected value></option>
					<option value="齐纳">齐纳</option>
					<option value="固晶">固晶</option>
					<option value="焊线">焊线</option>
					<option value="点胶">点胶</option>
					<option value="切割">切割</option>
					<option value="分光">分光</option>
					<option value="编带">编带</option>
				</select>
			</div>
			<label class="col-sm-1 col-form-label" for="inputDefault">班別:</label>
			<div class="col-sm-1">
				<input type="radio" class="form-check-input" name="classRadios" id="optionsRadios1" value="A"/>
				<label class="custom-control-label" for="customRadio1">白</label><br/>
				<input type="radio" class="form-check-input" name="classRadios" id="optionsRadios2" value="B"/>
				<label class="custom-control-label" for="customRadio1">夜</label>
			</div>
			<label class="col-sm-1 col-form-label" for="inputDefault">提示:</label>
			<div class="col-sm-4">
				<input readonly="" class="form-control"  id="ZT" type="text"/>
			</div>
			<!-- <button type="button" id="ChaXun" >查詢今天排配</button> -->
			<!-- 2018-3-14 16:27:31注釋:PHP要切開 -->
			<!-- <button type="button" id="ChaXun2" >查昨天B班排配</button> -->
			<!-- <button type="button" id="DaoChu" >導出</button> -->
		</div>	
		
		<div class="form-group row">
			<label class="col-sm-1 col-form-label" for="inputDefault">日期:</label>
			<div class="col-sm-2">
				<input readonly="" class="form-control"  id="RiQi" type="text"/>
			</div>

			<label class="col-sm-1 col-form-label" for="inputDefault">周別:</label>
			<div class="col-sm-1">
				<input readonly="" class="form-control"  id="ZhouBie" type="text"/>
			</div>

			<label class="col-sm-1 col-form-label" for="inputDefault">製表人:</label>
			<div class="col-sm-2">
				<input class="form-control"  id="ZhiBiao" type="text"/>
			</div>
			<button type="button" id="ChaXun" >查製表人</button>
		</div>
		
		<!-- 这里放筛选器 -->
		<!--button id="ShaiXuanBT">筛选机台号</button -->
		<div class="form-group row">
			<div class="col-sm-4">
				<input class="form-control" placeholder="筛选工單号"  id="CX1" type="text"/>
			</div>
			<div class="col-sm-4">
				<input class="form-control" placeholder="筛选机台号"  id="CX2" type="text"/>
			</div>
			<div class="col-sm-4">
				<input class="form-control" placeholder="筛选操作員"  id="CX3" type="text"/>
			</div>
		</div>
		<!-- <div id="ShaiXuan"></div> -->
		
		<!-- 这里是JExcel的表格位置 -->
		<div id="mytable"></div>
		
		<div class="form-group row">
			<div class="col-sm-2">
			</div>
			<div class="col-sm-2">
			</div>
			<div class="col-sm-2">
			</div>
			<div class="col-sm-2">
			</div>
			<div class="col-sm-2">
				<button type="button" id="keep" class="btn btn-primary btn-lg" >保存</button>
			</div>
			<!-- <div class="col-sm-2"> -->
				<!-- <button type="button" id="save" >提交</button> -->
			<!-- </div> -->
		</div>
		<div class="form-group row">
		</div>
		
		<p class="lead">
			<a class="btn btn-primary btn-lg" href="index.html" role="button">返回</a>
		</p>
	</div>
	
	<link rel="stylesheet" type="text/css" href="../jexcel-master/dist/css/jquery.jexcel.css" />
    <script src="../_vendor/jquery/dist/jquery.min.js"></script>
	<script src="../jexcel-master/dist/js/jquery.jexcel.js"></script>
    <script src="../_vendor/popper.js/dist/umd/popper.min.js"></script>
    <script src="../_vendor/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="../_assets/js/custom.js"></script>
	
<script>
	var CXdata = new Array;//最原始的查询数据
	var CXdata10 = new Array;//主动回车后的查询数据1
	var CXdata11 = new Array;//查询数据11
	var CXdata12 = new Array;//查询数据12
	var CXid1 = null;
	var json = {};
	
	var Plan_data = null; 
	
	var DuXie = [
		{ type:'text', readOnly:true },
		{ type:'text', readOnly:true },
		{ type:'text', readOnly:true },
		{ type:'text', readOnly:true },
		{ type:'text', readOnly:true },
		{ type:'text', readOnly:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true }
	];
	var LieMing = [
		'表深<br/>ID',
		'表头<br/>ID',
		'訂單號',
		'產品型號',
		'產品<br/>類型',
		'產品<br/>系列',
		'工单号',
		'机台号',
		'計劃<br/>產能<br/>(K)',
		'排配<br/>工時<br/>(分钟)',
		'操作员<br/>(姓名)',
		'实际<br/>產能<br/>(K)',
		'实际<br/>工时<br/>(分钟)',
		'UPH<br/>(K/H)',
		'待排配<br/>(待機)',
		'日點檢<br/>保養',
		'工單<br/>切換<br/>調机',
		'更換<br/>耗材',
		'冶具<br/>清理',
		'首件',
		'生產<br/>調机',
		'工程<br/>試樣',
		'設備<br/>故障<br/>停機',
		'品質<br/>異常<br/>停線',
		'待料',
		'水電氣<br/>異常',
		'其他<br/>計劃外<br/>停機',
		'未達成原因備註'
	];
	var LieKuan = [
		40,
		40,
		60,
		100,
		50,
		50,
		100,
		150,
		60,
		80,
		80,
		80,
		50,
		80,
		60,
		50,
		50,
		50,
		50,
		50,
		50,
		50,
		50,
		50,
		50,
		50,
		50,
		200
	];
	var $btn = $("#keep");
	
	var longdata = new Array;
	
	var date = new Date;
	var mm = "";
	var dd = "";
	
	var GongXu = encodeURI($("#WP").val());//工序
	var BanBie = $('input[type="radio"][name="classRadios"]:checked').val();//$("#optionsRadios2").val();//班別
	var ZhiBiao = encodeURI($("#ZhiBiao").val());//製表人
	
	//console.log(BanBie);//輸出undefined
	//	2018-3-16 11:55:49
   /*	JExcel的工作:
	*	禁止刪列:在刪除列時,提示禁止刪除列
	*	禁止移動列:在移動列時,提示禁止移動列
	*	機台號,工單號為空時,提示禁止為空
	*	行為空時,測試後端是否會接收到
	*/
	
	$('#mytable').jexcel({ data:Plan_data,
		// Allow column delete
		allowDeleteColumn:false,
		// Allow row dragging
		rowDrag:false,
		columns:DuXie,
		colHeaders:LieMing,
		colWidths:LieKuan
	});
	
	$(document).ready(function (){
		GongXu = encodeURI($("#WP").val());//工序
		BanBie = $('input[type="radio"][name="classRadios"]:checked').val();//班別
		ZhiBiao = encodeURI($("#ZhiBiao").val());//製表人
		if ($("#WP").val() === null || BanBie===undefined || ZhiBiao===""){
			$("#ZhiBiao").focus();
			alert("請選擇工序和班別并輸入製表人姓名!");
			$("#ZT").val("請選擇工序和班別并輸入製表人姓名!");
			return ;
		}
		//已剪到後面去了
	});
	
	$("#ChaXun").click(function(){
		GongXu = encodeURI($("#WP").val());//工序
		BanBie = $('input[type="radio"][name="classRadios"]:checked').val();//班別
		ZhiBiao = encodeURI($("#ZhiBiao").val());//製表人
		if ($("#WP").val() === null || BanBie===undefined || ZhiBiao===""){
			$("#ZhiBiao").focus();
			alert("請選擇工序和班別并輸入製表人姓名!");
			$("#ZT").val("請選擇工序和班別并輸入製表人姓名!");
			return ;
		}
		
		$.ajax({ url: "getPlanNEW.php?GongXu="+GongXu+"&BanBie="+BanBie+"&ZhiBiao="+ZhiBiao, //按製表人查詢
			dataType : "json",
			success: function(data){
				CXdata=data;
				Plan_data = data;
				$('#mytable').jexcel({ data:Plan_data,
					// Allow column delete
					allowDeleteColumn:false,
					// Allow row dragging
					rowDrag:false,
					columns:DuXie,
					colHeaders:LieMing,
					colWidths:LieKuan
				});
				//console.log(Plan_data);
			}
		});
	});
	
	$("#keep").click(function(){
		
	    GongXu = encodeURI($("#WP").val());//工序
		BanBie = $('input[type="radio"][name="classRadios"]:checked').val();//班別
		ZhiBiao = encodeURI($("#ZhiBiao").val());//製表人
		if ($("#WP").val() === null || BanBie===undefined || ZhiBiao===""){
			$("#ZhiBiao").focus();
			alert("請選擇工序和班別并輸入製表人姓名!");
			$("#ZT").val("請選擇工序和班別并輸入製表人姓名!");
			return ;
		}
		
		longdata = $('#mytable').jexcel('getData', false); 
		
		//JS FOR語法: for (var i=0,len=cars.length; i<len; i++)
		for (var i=0, len=longdata.length; i<len; i++){
			if(longdata[i][6] === "" || longdata[i][7] === ""){//6工單號或7機台號其中一個空白
				alert("不允許為空,請輸入工單號和機台號!");
				$("#ZT").val("不允許為空,請輸入工單號和機台號!");
				return ;
			}
		}

		$btn.attr({"disabled":true});//設置為禁用
		$btn.html("更新中,请不要重复点击,如果出不来就用谷歌浏览器!");
		//用逗點隔開的字串
		$.ajax({
			type:'POST',
			url: "keepNEW.php",//POST傳一個超長字串到後端
			data:"GongXu="+GongXu+"&BanBie="+BanBie+"&ZhiBiao="+ZhiBiao+"&ShuZu="+JSON.stringify(longdata),
			dataType : "json",
			success: function(data){
				$btn.html("更新完成!");
				alert("保存成功!");
				$("#ZT").val("保存成功!");
				$btn.attr({"disabled":false});//設置為啟用
				$btn.html("保存");
				//$btn.button('reset');//重置這個標籤為可點狀態
				CXdata=data;
				Plan_data = data;
				$('#mytable').jexcel({ data:Plan_data,
					// Allow column delete
					allowDeleteColumn:false,
					// Allow row dragging
					rowDrag:false,
					columns:DuXie,
					colHeaders:LieMing,
					colWidths:LieKuan
				});
			}
		});
	});
	
	$("#optionsRadios1").click(function(){
		if ($('input[type="radio"][name="classRadios"]:checked').val()==="A"){
			$("#RiQi").val(GetDateStr(0));//A不管怎麼樣就是今天
			$("#ZhouBie").val(getWeekOfYear(date));
		}
	})
	
	$("#optionsRadios2").click(function(){
		if ($('input[type="radio"][name="classRadios"]:checked').val()==="B"){
			if(date.getHours()<20){
				$("#RiQi").val(GetDateStr(-1));//B班還沒到20:00的時候就是昨天
				$("#ZhouBie").val(getWeekOfYear(date));
			}else{
				$("#RiQi").val(GetDateStr(0));//否則就是大於等於20:00,那麼久等於今天日期
				$("#ZhouBie").val(getWeekOfYear(date));
			}
		}
	})
	/*
	//change輸入框改變的方程
	$("#ZhiBiao").change(function(){
		if ($("#ZhiBiao").val()===""){
			$("#ZhiBiao").focus();
			alert("請輸入製表人姓名!");
			$("#ZT").val("請輸入製表人姓名!");
			return;
		}
	});
	*/
	/*
	//blur 失去焦點的事件
	$("#ZhiBiao").blur(function(){
		if ($("#ZhiBiao").val()===""){
			$("#ZhiBiao").focus();
			return;
		}
	});
	*/
	
	//湊默認日期(因為JS沒有日期格式化函數)
	
	//2018-3-8
	//2018-03-08
	
	$("#RiQi").val(GetDateStr(0));//今天日期
	
	$("#ZhouBie").val(getWeekOfYear(date));
	
	
	//一天含86,400,000毫秒(24*60*60*1000)
	//公式計算获取本周是一年中的第几周
	function getWeekOfYear(date){
	    var d1 = date;
	    var d2 = new Date(date.getFullYear(), 0, 1);
	    var d = Math.round((d1 - d2) / 86400000);
	    return Math.ceil((d + ((d2.getDay() + 1) - 1)) / 7);
	};
	
	//計算某天后的日期
	function GetDateStr(AddDayCount) {
	    var dd = new Date();
	    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
	    var y = dd.getFullYear();
	    var m = dd.getMonth()+1;//获取当前月份的日期
	    var d = dd.getDate();
	    return y+"-"+m+"-"+d;
	};
	//按下筛选按钮后运行
	$("#CX1").keypress(function(e){//
		var key = e.which;
		if (key == 13) {
			CXdata10 = $('#mytable').jexcel('getData', false);
			for(var i = 0, j = -1; i < CXdata10.length; i++){
				if (CXdata10[i][6]===$("#CX1").val() ){//循环判断工单是否=筛选器内容
					j += 1;//j=j+1(-1+1=0刚好是下标0)
					
					CXdata11[j] = CXdata10[i];//查询数据11的j行=查询1数据的i行
				}
			}
			
			$('#mytable').jexcel({data:CXdata11,
				// Allow column delete
				allowDeleteColumn:false,
				// Allow row dragging
				rowDrag:false,
				columns:DuXie,
				colHeaders:LieMing,
				colWidths:LieKuan
			});
		}
	});	
	
	$("#CX2").keypress(function(e){//
		var key = e.which;
		if (key == 13) {
			CXdata10 = $('#mytable').jexcel('getData', false);
			for(var i = 0, j = -1; i < CXdata10.length; i++){
				if (CXdata10[i][7]===$("#CX2").val() ){//循环判断工单是否=筛选器内容
					j += 1;//j=j+1(-1+1=0刚好是下标0)
					
					CXdata11[j] = CXdata10[i];//查询数据11的j行=查询1数据的i行
				}
			}
			
			$('#mytable').jexcel({data:CXdata11,
				// Allow column delete
				allowDeleteColumn:false,
				// Allow row dragging
				rowDrag:false,
				columns:DuXie,
				colHeaders:LieMing,
				colWidths:LieKuan
			});
		}
	});	
	
	$("#CX3").keypress(function(e){//
		var key = e.which;
		if (key == 13) {
			CXdata10 = $('#mytable').jexcel('getData', false);
			for(var i = 0, j = -1; i < CXdata10.length; i++){
				if (CXdata10[i][10]===$("#CX3").val() ){//循环判断工单是否=筛选器内容
					j += 1;//j=j+1(-1+1=0刚好是下标0)
					
					CXdata11[j] = CXdata10[i];//查询数据11的j行=查询1数据的i行
				}
			}
			
			$('#mytable').jexcel({data:CXdata11,
				// Allow column delete
				allowDeleteColumn:false,
				// Allow row dragging
				rowDrag:false,
				columns:DuXie,
				colHeaders:LieMing,
				colWidths:LieKuan
			});
		}
	});	
	


	//var html = "";
	/*
	$("#CX1").keypress(function(e){//
		var key = e.which;
		if (key == 13) {
			if ($("#CX1").val()===""){
				alert("不允许空值筛选!");
				$("#ZT").val("不允许空值筛选!");
				return;
			}
			CXdata10 = $('#mytable').jexcel('getData', false); 
			for(var i = 0, j = -1; i < CXdata10.length; i++){//按下按钮后,开始凑新的唯一值数组(用到的是字典功能)
				if(CXdata10[i][0] !== ""){
					json["'"+CXdata10[i][0]+"'"] = i;//这里循环把表深ID=行i存到字典里面
				}
				
				if (CXdata10[i][6]===$("#CX1").val() ){//循环判断工单是否=筛选器内容
					j += 1;//j=j+1(-1+1=0刚好是下标0)
					CXdata11[j] = CXdata10[i];//查询数据11的j行=查询1数据的i行
				}
			}
			<!-- console.log(CXdata11); -->
			$('#mytable').jexcel({data:CXdata11,
				// Allow column delete
				allowDeleteColumn:false,
				// Allow row dragging
				rowDrag:false,
				columns:DuXie,
				colHeaders:LieMing,
				colWidths:LieKuan
			});
		}
	});
	*/
	
	//change改变的事件,判断是否删掉,删掉就返回已更新的data
	/*
	$("#CX1").change(function(){//这里其实是要触发对CXdata的更新,然后最终以CXdata为主
		if ($("#CX1").val()===""){
			//循环更新长数据2的内容到CX1的数据里
			CXdata12 = $('#mytable').jexcel('getData', false); 
			for(var i = 0; i < CXdata12.length; i++){
				<!-- CXid1=CXdata12[i][0]; -->
				console.log("CXdata12="+CXdata12);
				<!-- console.log(CXdata12[i][0]); -->
				<!-- console.log(json["'"+CXid1+"'"]); -->
				
				CXdata[json["'"+CXdata12[i][0]+"'"]] = CXdata12[i];//从查询数据1取出
			}
			<!-- console.log(CXdata10); -->
			$('#mytable').jexcel({data:CXdata,
				// Allow column delete
				allowDeleteColumn:false,
				// Allow row dragging
				rowDrag:false,
				columns:DuXie,
				colHeaders:LieMing,
				colWidths:LieKuan
			});
		}
	});
	*/
</script>
	
</body>
</html>
